// vote.entity.ts
import {
  Entity,
  Column,
  PrimaryColumn,
  OneToMany,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Singer } from './singer.entity';
import * as crypto from 'crypto';
import { ApiProperty } from '@nestjs/swagger';
import { Expose, Transform } from 'class-transformer';
import { BaseEntity } from './base.entity';
import { CommentSinger } from './comment.entity';
import { Post } from './post.entity';
import { User } from './user.entity';

@Entity()
export class Poll extends BaseEntity {
  constructor() {
    super();
    this.id = crypto.randomBytes(8).toString('hex'); // 生成一个8字节（16个十六进制字符）的随机字符串作为id
  }
  @ApiProperty({
    required: false,
    description: '投票id',
  })
  @PrimaryColumn({ length: 225 })
  id: string;

  @ApiProperty({
    required: false,
    description: '投票名称',
  })
  @Column({ type: 'varchar', length: 255, comment: '投票名称' })
  name: string;

  @ApiProperty({
    required: false,
    description: '投票状态',
  })
  @Column({ type: 'int', nullable: true, default: 0 })
  status: number; // 投票状态，用数字表示，0表示待开始，1表示进行中，2表示已结束

  @ApiProperty({
    required: false,
    description: '投票类型',
  })
  @Column({ type: 'varchar', nullable: true, comment: '投票类型' })
  type: string;

  @ApiProperty({
    required: false,
    description: '投票内容',
  })
  @Column({ type: 'varchar', length: 255, nullable: true, comment: '投票内容' })
  content: string;

  @ApiProperty({
    required: false,
    description: '投票数量',
  })
  @Column({ type: 'varchar', length: 255, nullable: true, comment: '投票数量' })
  count: number;

  @ApiProperty({
    required: false,
    description: '是否发布',
  })
  @Column('tinyint', {
    nullable: true,
    default: 1,
    comment: '是否发布: 0:是 1:不是',
  })
  @Expose({ name: 'isLeaf' })
  @Transform(({ value }) => value === 1)
  published: number;

  @Column({ name: 'startTime', comment: '开始时间', nullable: true })
  @ApiProperty({
    description: '开始时间',
    required: false,
  })
  startTime: string;

  @Column({ name: 'endTime', comment: '结束时间', nullable: true })
  @ApiProperty({
    required: false,
    description: '结束时间',
  })
  endTime: string;

  @OneToMany(() => Singer, (singer) => singer.poll, {
    cascade: true, // 自动保存相关对象
    onDelete: 'SET NULL',
  })
  singers: Partial<Singer>[];

  @OneToMany(() => Post, (Post) => Post.poll, {
    cascade: true, // 自动保存相关对象
    onDelete: 'SET NULL',
  })
  post: Post;

  @ManyToOne(() => User, (user) => user.poll, {
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'userId' })
  user: User;

  @Column({ nullable: true })
  userId: string; // 将用户ID作为外键存储
  
}
